//
//  充话费.swift
//  民生生活
//
//  Created by apple on 2017/8/8.
//  Copyright © 2017年 youjindi.app.com. All rights reserved.
//

import UIKit
import AddressBook
import AddressBookUI

class PayForPhone: UIViewController {

    let textField = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "话费充值"
        setView()
    }
    
    var priceA:[String] = ["30元","50元","100元","200元","300元"]
    var buyPriceA:[String] = ["30","49.8","99.7","199.5","298"]
    func setView() -> Void {
        
        
        view.backgroundColor = UIColor.white
        
        let textL = UILabel()
        let alumBtn = UIButton()
        let line = UIView()
        
        textField.frame = CGRect(x: 10, y: 90, width: ScreenWidth - 80, height: 50)
        textField.placeholder = "请输入充值号码"
        textField.keyboardType = .phonePad
        textField.font = UIFont.systemFont(ofSize: 23)
        
        textL.frame = CGRect(x: 10, y: textField.frame.maxY + 5, width: 120, height: 23)
        textL.text = "账户绑定号码"
        textL.textColor = UIColor.lightGray
        textL.font = UIFont.systemFont(ofSize: 13)
        
        alumBtn.frame = CGRect(x: ScreenWidth - 60, y: 90, width: 44, height: 44)
        alumBtn.setImage(UIImage(named: "applogo"), for: .normal)
        
        alumBtn.reactive.controlEvents(.touchUpInside).observeValues { (btn) in
            let nav = ABPeoplePickerNavigationController()
            nav.delegate = self
            nav.predicateForEnablingPerson = NSPredicate(value: false)
            self.present(nav, animated: true, completion: nil)
        }
        
        line.frame = CGRect(x: 10, y: textL.frame.maxY + 10, width: ScreenWidth - 20, height: 0.7)
        line.backgroundColor = MainBackGroundColor
        
        view.addSubview(textField)
        view.addSubview(textL)
        view.addSubview(alumBtn)
        view.addSubview(line)
        
        let btnW = (ScreenWidth - 40)/3
        for index in 0..<priceA.count {
        
            var btnPoint = CGPoint(x: (btnW + 10) * CGFloat(index) + 10, y: line.frame.maxY + 10)
            
            if btnPoint.x + 80 > ScreenWidth{
                btnPoint = CGPoint(x: (btnW + 10) * CGFloat(index-3) + 10, y: line.frame.maxY + 100)
            }
            
            let btnFrame = CGRect(x: btnPoint.x, y: btnPoint.y, width: btnW, height: 80)
            let btn = PayForPhoneBtn(priceA[index],buyPriceA[index],btnFrame)
            view.addSubview(btn)
            
            btn.addTarget(self, action: #selector(PayForPhone.payForPhoneBtnCallBack(_:)), for: .touchUpInside)
            
        }
        
    }
    
    open func payForPhoneBtnCallBack(_ tap: UITapGestureRecognizer) -> Void {
        print("nano")
    }

}

extension PayForPhone: ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate{
    
    func peoplePickerNavigationControllerDidCancel(_ peoplePicker: ABPeoplePickerNavigationController) {
        
        peoplePicker.dismiss(animated: true, completion: nil)
    }
    
    func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord) {
        
        let personViewController = ABPersonViewController()
        personViewController.displayedPerson = person
        peoplePicker.pushViewController(personViewController, animated: true)
    }
    
    func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
        
        let phone = ABRecordCopyValue(person, kABPersonPhoneProperty) as ABMultiValue
        let index = ABMultiValueGetIndexForIdentifier(phone, identifier)
        var phoneNo = ABMultiValueCopyValueAtIndex(phone, index).takeRetainedValue()
        
        if phoneNo.hasPrefix("+"){
            phoneNo = (phoneNo as! NSString).substring(from: 3) as CFTypeRef
        }
        
        phoneNo = (phoneNo as! NSString).replacingOccurrences(of: "-", with: "") as CFTypeRef
    
        textField.text = (phoneNo as! String)
    }
    
    func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, shouldContinueAfterSelectingPerson person: ABRecord) -> Bool {
        return true
    }
    
    func peoplePickerNavigationController(_ peoplePicker: ABPeoplePickerNavigationController, shouldContinueAfterSelectingPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {
        return true
    }
    
}

class PayForPhoneBtn: UIControl {
    
    let topLabel = UILabel()
    let bottomLabel = UILabel()
    
    init(_ topText: String,_ bottom: String,_ frame: CGRect) {
        
        super.init(frame: frame)
        setView(topText, bottom)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        
        backgroundColor = MainColor
        topLabel.textColor = UIColor.white
        bottomLabel.textColor = UIColor.white
        
        return true
    }
    
    override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
        backgroundColor = UIColor.white
        topLabel.textColor = MainColor
        bottomLabel.textColor = MainColor
    }
    
    private func setView(_ topText: String,_ bottom: String) {
        
        topLabel.frame = CGRect(x: 0, y: 8, width: frame.width, height: 28)
        bottomLabel.frame = CGRect(x: 0, y: topLabel.frame.maxY + 8, width: frame.width, height: 23)
        
        addSubview(topLabel)
        addSubview(bottomLabel)
        
        isUserInteractionEnabled = true
        layer.cornerRadius = 7
        layer.borderColor = MainColor.cgColor
        layer.borderWidth = 0.7
        
        topLabel.textAlignment = .center
        topLabel.font = UIFont.systemFont(ofSize: 23)
        topLabel.textColor = MainColor
        topLabel.text = topText
        
        bottomLabel.textAlignment = .center
        bottomLabel.font = UIFont.systemFont(ofSize: 17)
        bottomLabel.textColor = MainColor
        bottomLabel.text = "售价: " + bottom + "元"
        
    }
}
